/*  
  -- The Lime II Project -- 

  A tuplespaces-based middleware for coordinating agents and mobile hosts.
  Copyright (C) 2005.
  Gian Pietro Picco, Amy L. Murphy, Lorenzo Bellini.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/
 
package testing;
import lime2.*;
import lights.adapters.*;
import lights.interfaces.*;
import java.util.Random;

/**
 * A simple agent installing a weak reaction that is triggered by template <code>String</code> and prints a message: 
 * allows the user to select mode and scope. 
 * 
 * @author Gian Pietro Picco
 * @author Amy Murphy
 * @author Lorenzo Bellini
 */

public class RemInst implements Runnable, ReactionListener 
{
 // identification number	
 private int id = new Random().nextInt(1000);		
	
 // private data members
 private Location cur = Location.HERE;
 private Location dest = Location.UNSPECIFIED;
 private byte mode = Reaction.ONCE;
 
 // String values for "here" and "unspecified"
 private final String HereS = "here";
 private final String UnspS = "?"; 
 private final String OnceS = "once";
 private final String AlltS = "all";	
 
 /**
  * Constructor method (default). Current location is here and destination location is unspecified; mode is once.
  */
 public RemInst()
 {
  // empty	
 }
 
 /**
  * Constructor method. Current location is here and destination location is unspecified.
  * 
  * @param a [mode = once, once-per-tuple]
  */
 public RemInst(String a)
 {
  if (a.equals(OnceS)) ; else if (a.equals(AlltS)) mode = Reaction.ONCEPERTUPLE; else	
  throw new IllegalArgumentException("mode must be " + OnceS + " or " + AlltS);
 }
 
 /**
  * Constructor method. Destination location is unspecified.
  * 
  * @param a [mode = once, once-per-tuple]
  * @param b [current location = unspecified, here]
  */
 public RemInst(String a, String b)
 {
  if (a.equals(OnceS)) ; else if (a.equals(AlltS)) mode = Reaction.ONCEPERTUPLE; else	
  throw new IllegalArgumentException("mode must be " + OnceS + " or " + AlltS);	
  if (b.equals(UnspS)) cur = Location.UNSPECIFIED;
  else
  if (!b.equals(HereS)) throw new IllegalArgumentException("cur must be " + UnspS + " or " + HereS);	
 }
 
 /**
  * Constructor method.
  * 
  * @param a [mode = once, once-per-tuple]
  * @param b [name/IP of current location] [current location = unspecified, here]
  * @param c [port of current location] [destination location = unspecified, here]
  */
 public RemInst(String a, String b, String c)
 {
  if (a.equals(OnceS)) ; else if (a.equals(AlltS)) mode = Reaction.ONCEPERTUPLE; else	
  throw new IllegalArgumentException("mode must be " + OnceS + " or " + AlltS);	
  if ((!b.equals(HereS) && !b.equals(UnspS)) || (!c.equals(HereS) && !c.equals(UnspS)))  
     cur = Location.build(b, new Integer(c).intValue());
  else 
  {
   if (b.equals(UnspS)) cur = Location.UNSPECIFIED;
   if (c.equals(HereS)) dest = Location.HERE;
  }
 }
 
 /**
  * Constructor method.
  * 
  * @param a [mode = once, once-per-tuple]
  * @param b [name/IP of current location] [current location = unspecified, here]
  * @param c [port of current location] [name/IP of destination location]
  * @param d [destination location = unspecified, here] [port of destination location]
  */
 public RemInst(String a, String b, String c, String d)
 {
  if (a.equals(OnceS)) ; else if (a.equals(AlltS)) mode = Reaction.ONCEPERTUPLE; else	
     throw new IllegalArgumentException("mode must be " + OnceS + " or " + AlltS);	
  if (b.equals(HereS)) dest = Location.build(c, new Integer(d).intValue()); 
  else
  if (b.equals(UnspS)) { cur = Location.UNSPECIFIED; dest = Location.build(c, new Integer(d).intValue()); }
  else
  if (d.equals(HereS)) { cur = Location.build(b, new Integer(c).intValue()); dest = Location.HERE; }
  else
  if (d.equals(UnspS)) cur = Location.build(b, new Integer(c).intValue());	
  else
  throw new IllegalArgumentException("if you pass only three parameters, cur or dest must be " + UnspS + "/" + HereS);
 }
 
 /**
  * Constructor method.
  * 
  * @param a [mode = once, once-per-tuple]
  * @param b [name/IP of current location]
  * @param c [port of current location]
  * @param d [name/IP of destination location]
  * @param e [port of destination location]
  */
 public RemInst(String a, String b, String c, String d, String e)
 {
  if (a.equals(OnceS)) ; else if (a.equals(AlltS)) mode = Reaction.ONCEPERTUPLE; else	
     throw new IllegalArgumentException("mode must be " + OnceS + " or " + AlltS);	
  cur = Location.build(b, new Integer(c).intValue());
  dest = Location.build(d, new Integer(e).intValue());
 }
 
 /**
  * Run method.
  */
 public void run()
 {
  System.out.println("RemInst " + id + ": started");     
  LimeTupleSpace lts = LimeTupleSpace.get("testing"); 
  ITuple pattern = new Tuple().add(new Field().setType(String.class));
  LimeTemplate template = new LimeTemplate(cur, dest, pattern);
  ReactionID[] rids = lts.addWeakReaction(new Reaction[] { new Reaction(mode, template, this) });
  System.out.println("RemInst " + id + ": installed " + rids[0]);
  System.out.println("RemInst " + id + ": dies");
 }
 
 /**
  * Reactive method.
  * 
  * @param re ReactionEvent
  */
 public void reactsToOut(ReactionEvent re)
 {	
  System.out.println("RemInst " + id + ": reaction fired on [" 
  		             + re.getLimeTuple().getID() + "] " 
					 + re.getLimeTuple().getTuple());	
 }

@Override
public void reactsToTake(ReactionEvent re) {
	// TODO Auto-generated method stub
	
}
 
}

